import {sendEmailGet, sendEmailPost} from "../api/api_email/email_api.js";
import userFriendGroupDao from "../dao/userfriendgroupDB.js";
import userDao from "../dao/userDB.js";
import groupBaseInfoDao from "../dao/groupbaseinfoDB.js";
//http返回统一格式
export const httpResult = (resultCode, msg, errMsg) => {
    let res =  {
        code: resultCode,
    };
    if(msg) {
        res.msg = msg;
    }
    if(errMsg) {
        res.code = 400;
        res.errMsg = errMsg;
    }
    return res;
};
httpResult.success = (msg) => {
    return {
        code: 200,
        msg
    };
};
httpResult.error = (errMsg) => {
    return {
        code: 400,
        errMsg
    };
};


//账号验证 靓号不让直接注册
export const isPriceAccount = (account) => {
    account = account.toString();
    return account.split("").filter(item => item === account[0]).length === account.length;

};
//随机生成账号
export const getRandomAccount = () => {
    let random = Math.random().toString();
    let account = random.slice(2, randomNum(7, 10) + 2);
    while(isPriceAccount(account)) {
        random = Math.random().toString();
        account = random.slice(2, randomNum(7, 10) + 2);
    }
    return account;
};
// 判断账号不再群和用户中
export const getUniAccount = async () => {
    let account;
    let isExisted = true;
    while (isExisted) {
        account = getRandomAccount();
        isExisted = (await userDao.isExisted(account)) || (await groupBaseInfoDao.isExistedByAccount(account));
    }
    return account;
};
//随机生成sessionID
export const getRandomSessionID = () => {
    let now = new Date();
    let year = now.getFullYear().toString();
    let month = (now.getMonth() + 1).toString().padStart(2, "0");
    let day = now.getDate().toString().padStart(2, "0");
    let hour = now.getHours().toString().padStart(2, "0");
    let min = now.getMinutes().toString().padStart(2, "0");
    return year + month + day + hour + min + randomNum(100000, 999999).toString();
};
export const getUniSessionID = async () => {
    let session_id = getRandomSessionID();
    let isExistedSessionID = true;
    while (isExistedSessionID) {
        session_id = getRandomSessionID();
        isExistedSessionID = await userFriendGroupDao.isExistedSessionID(session_id);
    }
    return session_id;
};
//随机[n,m]
export const randomNum = (minNum, maxNum) => {
    return parseInt(Math.random()*(maxNum-minNum+1)+minNum, 10);
};
//发送邮件
export const sendEmail = async (to, content) => {
    let USER = "wufeng.1998@qq.com";
    let PASS = "irtujcfzqqlhhgae";
    let FORM = "WuFeng 无风邮件发送中心 <wufeng.1998@qq.com>";
    let TITLE = "WuFeng 邮件中心";
    if (typeof to !== "string") {
        to = to.join(",");
    }
    let sendResult = await sendEmailGet(USER, PASS, to, FORM, TITLE, content);
    return sendResult.data;
};
export const sendEmail_post = async (to, content) => {
    let USER = "wufeng.1998@qq.com";
    let PASS = "irtujcfzqqlhhgae";
    let FORM = "WuFeng 无风邮件发送中心 <wufeng.1998@qq.com>";
    let TITLE = "WuFeng 邮件中心";
    if (typeof to !== "string") {
        to = to.join(",");
    }
    let sendResult = await sendEmailPost(USER, PASS, to, FORM, TITLE, content);
    return sendResult.data;
};
//随机生成验证码
export const randomCaptcha = () => {
    return randomNum(100000, 999999);
};
//现在的时间戳
export const getNow = () => {
    return new Date().getTime();
};
